import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class Index extends StatefulWidget {
  Index({Key key}) : super(key: key);

  _IndexState createState() => _IndexState();
}

Future getData () async {
  const httpHeaders = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    'Cookie': '_ga=GA1.2.553531781.1555553293; _gid=GA1.2.1926651818.1561105456; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1561105456; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1561105507; SERVERID=3431a294a18c59fc8f5805662e2bd51e|1561105515|1561105453; _gat=1',
    'Host': 'time.geekbang.org',
    'Origin': 'https://time.geekbang.org',
    'Pragma': 'no-cache',
    'Referer': 'https://time.geekbang.org/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
};
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.headers = httpHeaders;
    response = await dio.get('https://time.geekbang.org/serv/v1/column/newAll');
    print(response);
    return response.data;
  } catch (e) {
    return print(e);
  }
}
class _IndexState extends State<Index> {
  String desc = '还没有请求数据';
  void _jike () {
    getData().then((value) {
      setState(() {
       desc = value['data'].toString(); 
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Scaffold(
         body: Container(
           child: Column(children: <Widget>[
             RaisedButton(
               onPressed: _jike,
               child: Text('请求数据'),
             ),

             Text(desc)
           ],),
         ),
       ),
    );
  }
}